package com.yaDuo.common.util;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.viewbinding.ViewBinding;



import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class VBUtil {

    public static <VB extends ViewBinding> VB createViewBindingFromGenericSuperclass(
            Class<?> classOfT, LayoutInflater inflater) {
        return createViewBindingFromGenericSuperclass(classOfT, inflater, null, false);
    }

    @SuppressWarnings("All")
    public static <VB extends ViewBinding> VB createViewBindingFromGenericSuperclass(
            Class<?> classOfT, LayoutInflater inflater, ViewGroup parent,
            boolean attachToParent) {

        ParameterizedType type = null;
        Class<VB> vbClass = null;

        while (true) {//处理多次继承
            String className = classOfT == null ? "" : classOfT.getName();
            if (className == null || classOfT == Object.class || className.startsWith("android") || className.startsWith("java")) {
                new IllegalStateException("没有找到该类父类的ViewBinding泛型").printStackTrace();
                return null;
            }

            Type genericSuperclass = classOfT.getGenericSuperclass();
            if (genericSuperclass instanceof ParameterizedType) {
                ParameterizedType superclassParameterizedType = (ParameterizedType) genericSuperclass;
                Type[] genericTypes = superclassParameterizedType.getActualTypeArguments();
                boolean haveVB = false;
                for (Type genericType : genericTypes) {
                    if (genericType instanceof Class) {//还有可能是别的ParameterizedType,即泛型的类型也是一个泛型
                        if (ViewBinding.class.isAssignableFrom((Class) genericType)) {//泛型中有ViewBinding
                            haveVB = true;
                            vbClass = (Class<VB>) genericType;
                            break;
                        }
                    }
                }
                if (haveVB) {
                    break;
                } else {
                    classOfT = classOfT.getSuperclass();
                }
            } else {
                classOfT = classOfT.getSuperclass();
            }
        }
        return createViewBinding(vbClass, inflater, parent, attachToParent);
    }

    public static <VB extends ViewBinding> VB createViewBinding(Class<VB> vbClass, LayoutInflater inflater) {
        return createViewBinding(vbClass, inflater, null, false);
    }

    @SuppressWarnings("All")
    public static <VB extends ViewBinding> VB createViewBinding(
            Class<VB> vbClass, LayoutInflater inflater, ViewGroup parent,
            boolean attachToParent) {
        try {
            //防止混淆
            Method[] methods = vbClass.getDeclaredMethods();
            for (Method method : methods) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                Class<?> returnType = method.getReturnType();
                boolean isStatic = (method.getModifiers() & Modifier.STATIC) == Modifier.STATIC;
                if (returnType == vbClass && isStatic && parameterTypes != null) {
                    if (parameterTypes.length == 3 && parameterTypes[0] == LayoutInflater.class
                            && parameterTypes[1] == ViewGroup.class
                            && (parameterTypes[2] == boolean.class || parameterTypes[2] == Boolean.class)) {
                        return (VB) method.invoke(null, inflater, parent, attachToParent);
                    } else if (parameterTypes.length == 2 && parameterTypes[0] == LayoutInflater.class
                            && parameterTypes[1] == ViewGroup.class) {//布局根元素为merge时，只会有两个参数
                        return (VB) method.invoke(null, inflater, parent);
                    }
                }
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}